package com.pshcp.doctor.tool;

import com.framework.base.BaseDTO;
import com.framework.base.PageList;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.diagnosis.DoctorDiagnosisPageAO;
import com.pshcp.doctor.ao.diagnosis.DoctorDiagnosisVO;
import com.pshcp.doctor.vo.diagnosisevaluation.DoctorDiagnosisEvaluationVO;
import com.pshcp.domain.dto.PatientOnlineOrderDTO;
import com.pshcp.domain.model.DoctorDiagnosisEvaluation;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.service.PatientService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/6 21:38
 */
public class DoctorDiagnosisTool {
    public static PatientOnlineOrderDTO buildGetDoctorSendMessagePage(DoctorDiagnosisPageAO doctorDiagnosisPageAO) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(doctorDiagnosisPageAO.getPageNum(), doctorDiagnosisPageAO.getPageSize());
        PatientOnlineOrderDTO patientOnlineOrderDTO = new PatientOnlineOrderDTO();
        patientOnlineOrderDTO.setPageNum(baseDTO.getPageNum());
        patientOnlineOrderDTO.setPageSize(baseDTO.getPageSize());
        patientOnlineOrderDTO.setSearch(doctorDiagnosisPageAO.getSearch());
        patientOnlineOrderDTO.setDoctorId(ReqComTool.getUserId());
        patientOnlineOrderDTO.setPayStatus(2);
        patientOnlineOrderDTO.setBizStatus(doctorDiagnosisPageAO.getBizStatus() == null ? 0 : doctorDiagnosisPageAO.getBizStatus());
        return patientOnlineOrderDTO;
    }

    public static PageList<DoctorDiagnosisVO> buildGetDoctorDiagnosisPage(PatientService patientService, PageList<PatientOnlineOrder> pageList) {
        List<PatientOnlineOrder> items = pageList.getItems();
        List<Long> patientIds = items.stream().map(PatientOnlineOrder::getPatientId).collect(Collectors.toList());
        List<Patient> patientList = patientService.findByIds(patientIds);
        Map<Long, Patient> patientMap = patientList.stream().collect(Collectors.toMap(Patient::getId, patient -> patient));
        List<DoctorDiagnosisVO> list = new ArrayList<>();
        for (PatientOnlineOrder item : items) {
            DoctorDiagnosisVO doctorDiagnosisVO = CommonTool.copyProperties(item, DoctorDiagnosisVO.class);
            Patient patient = patientMap.get(item.getPatientId());
            doctorDiagnosisVO.setPatientName(patient == null || StringUtils.isBlank(patient.getName()) ? "未知" : patient.getName().substring(0, 1) + "**");
            list.add(doctorDiagnosisVO);
        }
        PageList<DoctorDiagnosisVO> page = new PageList<>();
        page.setPages(pageList.getPages());
        page.setPageNum(pageList.getPageNum());
        page.setPageSize(pageList.getPageSize());
        page.setTotal(pageList.getTotal());
        page.setItems(list);
        return page;
    }
}
